﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using BusinessLogic;
using System.Data;
using System.Text;

namespace Interface
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        DataConnection dc = new DataConnection();
        

        //string cnstr = ConfigurationManager.ConnectionStrings["QLNTConStr"].ToString();
        
        //public void ExecNonQuery(string query)
        //{

        //    SqlConnection cn = new SqlConnection(cnstr);
        //    SqlCommand command = new SqlCommand(query, cn);
        //    command.Connection.Open();
        //    command.ExecuteNonQuery();
        //    command.Connection.Close();
        //}
        protected void Page_Load(object sender, EventArgs e)
        {            
            checkSession();                        
            Load_District();
            Load_Posts();

        }

        protected void checkSession()
        {
            if (Session["AdminSession"] != null || Session["OwnerSession"]!=null || Session["CustomerSession"]!=null)
            {
                ibSignIn.Visible = false;
                ibSignUp.Visible = false;
                                                 
                lblWelcome.Visible = true;
                ibSignOut.Visible = true;
                string userName = "";                               
                if (Session["AdminSession"]!=null)
                {
                    StringBuilder strBuilder = new StringBuilder();
                    strBuilder.Append("<td class='Hori-me-detail'><a class='link-color' style='text-decoration:none' href='/Admin.aspx'>Admin</a></td>");
                    litAdmin.Text = strBuilder.ToString();
                    userName = Session["AdminSession"].ToString();
                }
                else if (Session["OwnerSession"] != null)
                {
                    userName = Session["OwnerSession"].ToString();
                }
                else
                {
                    userName = Session["CustomerSession"].ToString();
                }
                lblWelcome.Text = "Xin chào " + userName;
            }
            else
            {                                                                
                ibSignIn.Visible = true;

                lblWelcome.Visible = false;

                ibSignOut.Visible = false;
                lblWelcome.Text = string.Empty;
            }
        }

      

        private void Load_District()
        {
            string sqlStr1 = "Select * from District where IdCity=" + 1 +"";            
            DataTable dt = dc.getData(sqlStr1);
            dtlDistrict.DataSource = dt;
            dtlDistrict.DataBind();
        
        }

        private void Load_Posts()
        {
            string sqlStr = "Select TOP 5 * from Post where PostStatus=N'Xuất bản' Order By Viewed DESC";
            DataTable dt = dc.getData(sqlStr);
            StringBuilder strBuilder = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["PostID"].ToString());
                    string imageSource = "/Images/Post/" + row["ImageSource"].ToString();
                    string title = row["Title"].ToString();
                    string link = "/Posts/PostDetails.aspx?ID="+ID;


                    strBuilder.AppendFormat(@"<div class='tin-pre-border'>
                                                <div class='tin-pre'>
                                                    <img width='65' height='61' src='{0}'>
                                                </div>
                                                <a class='link-color' href='{1}'>{2}</a>                                            
                                            </div>
                                        <div class='small-pace'></div>", imageSource, link, title);
                    lit.Text = strBuilder.ToString();
                }
            }            
            
        }

        //protected DataTable dataTableInitiate()
        //{
        //    DataTable dt = new DataTable();
        //    DataColumn col = dt.Columns.Add("ID", typeof(int));
        //    col.AutoIncrement = true;
        //    col.AutoIncrementStep = 1;
        //    col.AutoIncrementSeed = 1;
        //    dt.Columns.Add("Image", typeof(string));
        //    dt.Columns.Add("Title", typeof(string));           
        //    return dt;
        //}

               
        protected void ibSignIn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Login.aspx");
        }

        protected void ibSignOut_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/LogOut.aspx");
        }

        protected void ibSignUp_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Users/Register.aspx");
        }
        
    }
}